package com.huizhonglm.diap.widget.expandTab;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.core.utils.ToastUtil;
import com.huizhonglm.diap.widget.NoScrollGridView;

import java.util.List;


public class MenuGridSignle extends RelativeLayout implements MenuBaseAction {

	private MenuInfo[] items;//显示字段
	private OnSelectListener mOnSelectListener;
	private String showText = "item1";
	private int showPosition = -1;

	public String getShowText() {
		return showText;
	}

	public MenuGridSignle(Context context, List<MenuInfo> groups) {
		super(context);
		init(context, groups);
	}

	public MenuGridSignle(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context, null);
	}

	public MenuGridSignle(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, null);
	}

	private void init(Context context, List<MenuInfo> groups) {
		if (!EmptyUtil.isEmpty(groups)) {
			items = new MenuInfo[groups.size()];
			for (int i =0; i< groups.size(); i++) {
				MenuInfo info = groups.get(i);
				items[i] = info;
			}
		}

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.widget_extab_grid_signle, this, true);
		setBackgroundResource(R.drawable.bg_dd_menu);
		Button submitBtn = (Button) findViewById(R.id.submit_btn);
		NoScrollGridView gridView = (NoScrollGridView) findViewById(R.id.filter_grid);
		MenuGridAdapter adapter = new MenuGridAdapter(context, items);
		adapter.setTextSize(17);
		gridView.setAdapter(adapter);
		submitBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (showPosition >= 0){
					if (mOnSelectListener != null) {
						mOnSelectListener.getValue(items[showPosition]);
					}
				} else {
					ToastUtil.makeTextShort("请选择筛选项");
				}
			}
		});
		adapter.setOnItemClickListener(new MenuGridAdapter.OnItemClickListener() {

			@Override
			public void onItemClick(View view, int position) {
				showPosition = position;
				if (position < 0) {
					showText = "";
				} else {
					showText = items[position].getName();
				}
			}
		});
	}

	public void setOnSelectListener(OnSelectListener onSelectListener) {
		mOnSelectListener = onSelectListener;
	}

	public interface OnSelectListener {
		void getValue(MenuInfo info);
	}

	@Override
	public void hide() {
		
	}

	@Override
	public void show() {
		
	}

}
